<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'bas:team:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.name }}
        </a>
      </template>
      
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasTeamList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { teamDelete, teamListData } from '/@/api/bas/team';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('bas.team');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('班组档案'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('班组名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },

    
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('班组编号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('班组名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 150,
      align: 'center',
      slot: 'firstColumn',
    },
    {
      title: t('上班时间'),
      dataIndex: 'toworkdate',
      key: 'a.toworkdate',
      sorter: true,
      width: 150,
      align: 'center',
      slot: 'toworkdate',
    },
    {
      title: t('下班时间'),
      dataIndex: 'offworkdate',
      key: 'a.offworkdate',
      sorter: true,
      width: 150,
      align: 'center',
      slot: 'toworkdate',
    },
    {
      title: t('开始休息时间'),
      dataIndex: 'strestdate',
      key: 'a.strestdate',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('结束休息时间'),
      dataIndex: 'edrestdate',
      key: 'a.edrestdate',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('工作时长'),
      dataIndex: 'workhours',
      key: 'a.workhours',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 150,
      align: 'center',
      dictType: 'sys_status',
    },
    {
      title: t('修改日期'),
      dataIndex: 'updateDate',
      key: 'a.updateDate',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 150,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑角色'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'bas:team:edit',
      },
      
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除角色'),
        popConfirm: {
          title: t('确认要删除该班组档案吗？'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        auth: 'bas:team:edit',
      },
    ],
   
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: teamListData,
    beforeFetch: (params) => {
      return params;
    },
  
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

 

  async function handleDelete(record: Recordable) {
    const res = await teamDelete(record);
    showMessage(res.message);
    handleSuccess();
  }


  function handleSuccess() {
    reload();
  }
</script>
